import { Application } from "egg";
import { ObjectId } from "mongoose";
import * as AutoIncrementFactory from "mongoose-sequence";
export interface BusinessCompanyProps {
  _id: ObjectId;
  name: string; // 商业公司名称
  company_id: ObjectId; // 所属代理商
  is_delete: boolean; // 是否删除
}
function initBusinessCompanyModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const BusinessCompanyScheme = new Schema<BusinessCompanyProps>(
    {
      name: { type: String, unique: true, required: true },
      company_id: {
        type: Schema.Types.ObjectId,
        ref: "Company",
        required: true,
      },
      is_delete: { type: Boolean, default: false },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  BusinessCompanyScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "BusinessCompany_counter",
  });
  return mongoose.model<BusinessCompanyProps>(
    "BusinessCompany",
    BusinessCompanyScheme
  );
}

export default initBusinessCompanyModel;
